package cn.cxj.pet.controller;

import cn.cxj.basic.exception.BusinessException;
import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LoginContext;
import cn.cxj.basic.util.PageList;
import cn.cxj.pet.domain.Pet;
import cn.cxj.pet.domain.SearchMasterMsg;
import cn.cxj.pet.query.SearchMasterMsgQuery;
import cn.cxj.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            System.out.println(searchMasterMsg);
            seachMasterMsgService.publish(searchMasterMsg, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试!");
        }
    }

    /**
     * 查询待处理寻主消息
     *
     * @param query
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        System.out.println(LoginContext.getCurrentLogininfo(request));
        return seachMasterMsgService.pending(query, LoginContext.getCurrentLogininfo(request));

    }

    /**
     * 查询已处理寻主消息
     *
     * @param query
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return seachMasterMsgService.processed(query, LoginContext.getCurrentLogininfo(request));

    }


    /**
     * 处理寻主消息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试!");
        }
    }

}
